package com.cr0ssVtW.arrowlimiter;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.HashMap;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.util.config.Configuration;

public class ArrowLimiterPlayerListener extends PlayerListener
{
	private ArrowLimiter arrLim = new ArrowLimiter();
	double delay;
	double meleedelay;

	public ArrowLimiterPlayerListener(ArrowLimiter parent)
	{
		this.arrLim = parent;
	}

	public void onPlayerInteract(PlayerInteractEvent event)
	{
		this.delay = (this.arrLim.config.getDouble("timedelay", 0.1D) * 1000.0D);
		this.meleedelay = (this.arrLim.config.getDouble("timedelay", 0.1D) * 1000.0D);

		Player archer = event.getPlayer();
		Player melee = event.getPlayer();

		if(event.hasBlock()) 
			return;
 
		if ((event.getItem().getType() == Material.BOW) && (event.getPlayer().getInventory().contains(Material.ARROW)))
		{
			if (((event.isCancelled() & event.getAction() != Action.RIGHT_CLICK_AIR)))
				return;
		
			if (this.arrLim.timeLimit.get(archer) == null)
				this.arrLim.timeLimit.put(archer, Double.valueOf(0.0D));
		
			PlayerInventory inv = archer.getInventory();

			ItemStack harmor = inv.getHelmet();
			ItemStack carmor = inv.getChestplate();
			ItemStack larmor = inv.getLeggings();
			ItemStack barmor = inv.getBoots();

			double k = this.delay;
			k = this.delay;

			if ((harmor.getTypeId() == 298) && (carmor.getTypeId() == 299) && (larmor.getTypeId() == 300) && (barmor.getTypeId() == 301))
				k *= 0.3D;
			else
			{
				if (carmor.getTypeId() == 299)
					k *= 0.7D;

				if (larmor.getTypeId() == 300)
					k *= 0.7D;
        
				if (harmor.getTypeId() == 298)
					k *= 0.9D;
        
				if (barmor.getTypeId() == 301)
					k *= 0.9D;
			}

			double now = Calendar.getInstance().getTimeInMillis();
			double left = ((Double)this.arrLim.timeLimit.get(archer)).doubleValue() + k - now;

			DecimalFormat df = new DecimalFormat("#.##");
			if (left > 0.0D)
			{
				event.getPlayer().sendMessage("You have to wait " + df.format(k / 1000.0D) + " second(s) between arrows");
				event.setCancelled(true);
				event.getPlayer().updateInventory();
				now = Calendar.getInstance().getTimeInMillis();

				return;
			}

			this.arrLim.timeLimit.put(archer, Double.valueOf(now));
		  
		}
	   
		event.getPlayer().sendMessage("HI");
	  
		if (((event.getItem().getType() == Material.IRON_SWORD) || (event.getItem().getType() == Material.STONE_SWORD) || (event.getItem().getType() == Material.WOOD_SWORD) || (event.getItem().getType() == Material.GOLD_SWORD) || (event.getItem().getType() == Material.DIAMOND_SWORD)) && (event.getAction() == Action.LEFT_CLICK_AIR))
		{
			event.getPlayer().sendMessage("in sword code");
			
			if (((event.isCancelled() & event.getAction() != Action.LEFT_CLICK_AIR)))
				return;
		
			if (this.arrLim.timeLimit.get(melee) == null)
				this.arrLim.timeLimit.put(melee, Double.valueOf(0.0D));

			double r = this.meleedelay;
			r = this.meleedelay;

			r *= 1.0D;


			double meleenow = Calendar.getInstance().getTimeInMillis();
			double meleeleft = ((Double)this.arrLim.timeLimit.get(melee)).doubleValue() + r - meleenow;

			DecimalFormat df = new DecimalFormat("#.##");
			if (meleeleft > 0.0D)
			{
				event.getPlayer().sendMessage("You have to wait " + df.format(r / 1000.0D) + " second(s) between arrows");
				event.setCancelled(true);
				event.getPlayer().updateInventory();
				meleenow = Calendar.getInstance().getTimeInMillis();

				return;
			}

			this.arrLim.timeLimit.put(melee, Double.valueOf(meleenow));
			return;

		}
	}
}
